home *** CD-ROM | disk | FTP | other *** search
- Path: gas.physics.usu.edu!pjh
- Newsgroups: comp.lang.c++
- Subject: operator<< precedence, cout, and weirdness
- Message-ID: <1996Apr12.133813.78123@cc.usu.edu>
- From: pjh@gas.physics.usu.edu (Paul Hepworth)
- Date: 12 Apr 96 13:38:13 MDT
- Organization: Utah State University
- Nntp-Posting-Host: gas.physics.usu.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- I'm having a hard time figuring out the weird eval order
- of these operator<< expressions:
-
- Here is the output from the prog below:
- 2220
- 2220
-
- It appears the i++/++i expressions are being evaluated right to
- left instead of left to right.
- I included a parenthesized version to explicitly specify
- the order I think it should already be evaluated in, and it
- gives the same result.
-
- What's the deal?!
-
- #include <stream.h>
- int main()
- {
- int i=0;
-
- cout << i++ << i << ++i << i++ << endl;
- cout << i << endl;
-
- i=0;
- (((( cout << i++) << i) << ++i) << i++) << endl;
- cout << i << endl;
- }
-
- I think it should evaluate as follows:
- i++, producing integer result
- cout << above result, producing istream&
- above result << i, producing istream&
- ++i, producing integer
- above istream& << above integer result, producing istream&
- i++, producing integer
- above istream& << above integer, producing istream&
- above istream& << endl, producing istream&
-
- instead, it seems to evaluate the i++/++i expressions to temporaries,
- then cout << tmp1 << tmp2....
-
- BTW, with numbers, it evaluates as expected:
- 1 << 1 << 2 produces 8, not 16
-
-
- Paul
-